

/** 判断是否是OAuth2APP环境 */
export function isOAuth2AppEnv() {
    // #ifdef H5
    return /wxwork|dingtalk/i.test(navigator.userAgent)
    // #endif
    return false;
}

// 获取url中的参数
export const getUrlParams = (url) => {
    let result = {
        url: '',
        params: {}
    };
    let list = url.split('?');
    result.url = list[0];
    let params = list[1];
    if (params) {
        let list = params.split('&');
        list.forEach(ele => {
            let dic = ele.split('=');
            let label = dic[0];
            let value = dic[1];
            result.params[label] = value;
        });
    }
    return result;
};

//分享到微信
export const shareToWx = ()=>{
	console.log(wx.canIUse("share"))
	uni.share({
		provider: "weixin",
		scene: "WXSceneTimeline",
		type: 0,
		href: "http://uniapp.dcloud.io/",  //跳转链接
		title: "uni-app分享",
		summary: "我正在使用HBuilderX开发uni-app，赶紧跟我一起来体验！", //摘要
		imageUrl: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png",
		success: function (res) {
			console.log("success:" + JSON.stringify(res));
		},
		fail: function (err) {
			console.log("fail:" + JSON.stringify(err));
		}
	});
}

//分享到朋友圈
export const shareToCircle = ()=>{
	
}
export const rpxToPx = (value) => {
	const system = uni.getSystemInfoSync()
	//设备宽度 / uni-app规定屏幕基准宽度为750rpx * rpx
	const px = system.windowWidth / 750 * value;
	return px;
}
export const pxToRpx = (value) => {
	const system = uni.getSystemInfoSync()
	// uni-app规定屏幕基准宽度为750rpx / 设备宽度px * px
	const rpx = 750 / system.windowWidth * value;
	return rpx;
}
export const debounce = (fn,timeSencond,immdiate) =>{
	let time = null;
	return (...arg)=>{
		clearTimeout(time);
		if(immdiate){
			immdiate = false;
			fn.apply(null,arg)
		}
		time = setTimeout(()=>{
			fn.apply(null,arg)
		},timeSencond)
	}
}
export const throttle = (fn,timeSencond,immdiate) =>{
	let time = null;
	return (...arg)=>{
		if(time){
			return;
		}
		if(immdiate){
			immdiate = false;
			fn.apply(null,arg)
		}
		time = setTimeout(()=>{
			time = null;
			fn.apply(null,arg)
		},timeSencond)
	}
}
// 保存文件
 export const  fileSave = (tempFilePath, myPath)=> {
    return new Promise(function (resolve, reject) {
      const fileManager = wx.getFileSystemManager(); // 文件系统
      fileManager.saveFile({
        tempFilePath: tempFilePath, // 临时文件路径
        filePath: myPath,  // 文件夹路径 + 文件名
        success: function (res) {
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  }

  // 创建文件夹
 export const fileMkdir = (myPath)=> {
    return new Promise(function (resolve, reject) {
      const fileManager = wx.getFileSystemManager(); // 文件系统
      fileManager.mkdir({
        dirPath: myPath, // 文件夹路径
        success: function (mkdir) {
          resolve(mkdir)
        },
        fail: function (mkdirerr) {
			console.log('fileMkdir err',err)
          reject(mkdirerr)
        }
      })
    })
  }

  // 判断文件夹是否存在
 export const fileExist = (myPath) => {
    return new Promise(function (resolve, reject) {
      const fileManager = wx.getFileSystemManager(); // 文件系统
      fileManager.access({
        path: myPath,  // 文件夹路径
        success: function (exist) {
          resolve(exist)
        },
        fail: function (err) {
			console.log('fileExist err',err)
          reject(err)
        }
      })
    })
  }


